package com.zy.fee.roadpark.activity.nb;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.dao.CommonLogic;
import com.zy.fee.roadpark.dao.NbNodeLogic;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.model.CommonResult;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.LibraryHelper;
import com.zygk.library.util.StringUtils;
import com.zygk.library.util.ToastUtil;
import java.util.ArrayList;
import java.util.UUID;

/**
 * 故障申报
 * Created by qhj on 2018/9/14 0014.
 */

public class FaultDeclareActivity extends BaseActivity {
  @BindView(R.id.lh_tv_title) TextView lhTvTitle;
  @BindView(R.id.iv_photo) ImageView ivPhoto;
  @BindView(R.id.et_code) EditText etCode;
  @BindView(R.id.et_problem) EditText etProblem;

  private ArrayList<ImageItem> images = null;
  private String picPath = "";

  @Override public void setContentView() {
    setContentView(R.layout.activity_fault_declare);
  }

  @Override public void init() {
    initData();
    initView();
    initListener();
  }

  private void initData() {
    imageManager.loadResImage(R.mipmap.icon_photo, ivPhoto);
  }

  private void initView() {
    lhTvTitle.setText("故障申报");
  }

  private void initListener() {

  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }

  @OnClick({ R.id.ll_back, R.id.iv_photo, R.id.rtv_submit }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.ll_back:
        finish();
        break;
      case R.id.iv_photo:
        initImagePickerSingle(false);
        picOne();
        break;
      case R.id.rtv_submit:
        nbnode_fault_add();
        break;
    }
  }

  private void nbnode_fault_add() {
    if (StringUtils.isBlank(picPath)) {
      ToastUtil.showMessage(mContext, "请先拍照上传图片");
      return;
    }
    if (StringUtils.isBlank(etCode.getText().toString())) {
      ToastUtil.showMessage(mContext, "请输入车位号");
      return;
    }
    if (StringUtils.isBlank(etProblem.getText().toString())) {
      ToastUtil.showMessage(mContext, "请详细描述故障问题");
      return;
    }

    String fileName = UUID.randomUUID().toString() + picPath.substring(picPath.lastIndexOf("."));

    CommonLogic.do_upload_pic(mContext, true, picPath, fileName, new HttpRequest.HttpCallback() {
      @Override public void onStart() {
        showPd();
      }

      @Override public void onSucceed(Object obj) {
        CommonResult result = (CommonResult) obj;
        NbNodeLogic.nbnode_fault_add(mContext, LibraryHelper.userManager().getUserID(),
            etCode.getText().toString(), etProblem.getText().toString(), result.getReUrlPath(),
            new HttpRequest.HttpCallback() {
              @Override public void onStart() {
                showPd();
              }

              @Override public void onSucceed(Object obj) {
                ToastUtil.showMessage(mContext, "故障申报成功");
                finish();
              }

              @Override public void onFailed() {
                ToastUtil.showNetErrorMessage(mContext);
              }

              @Override public void onFinish() {
                dismissPd();
              }
            });
      }

      @Override public void onFailed() {
        dismissPd();
      }

      @Override public void onFinish() {

      }
    });
  }

  public void onActivityResult(int requestCode, int resultCode, final Intent intent) {
    if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
      if (intent != null && requestCode == REQUEST_CODE_SELECT) {
        images = (ArrayList<ImageItem>) intent.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
        picPath = images.get(0).path;
        imageManager.loadLocalImage(picPath, ivPhoto);
      }
    }
  }
}
